package celiasoft.itasalib.query.resultparser;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import celiasoft.itasalib.query.ResultsMetaInfo;
import celiasoft.itasalib.structures.DataElement;
import celiasoft.itasalib.structures.Subtitle;

public class QuerySingleShowSubResultParser extends IQueryResultParser {

	@SuppressWarnings("unused")
	private static final String TAG = "itasalib.parser.QuerySingleShowSubResultPaser";

	public List<DataElement> parse(Document doc, ResultsMetaInfo meta) {
		
		ArrayList<DataElement> res = new ArrayList<DataElement>();

		NodeList news =  doc.getElementsByTagName("subtitle"); 
		
		if(news.getLength() < 1 
				|| !news.item(0).hasChildNodes()){
			return res;
		}
		
		Node node = news.item(0);
						
		DataElement el = parseNode(node);
		if(el != null){
			res.add(el);
		}
				
		return res;
	}

	private DataElement parseNode(Node n) {
		
		Element e = (Element)n;
		
		int id = -1;
		id = W3CParsingHandler.parseIntValue(e, "id");
		if(id == -1)
			return null;
		
		Subtitle el = new Subtitle(id);
		
		el.setName(DomHandler.getValue(e, "name"));
		el.setVersion(W3CParsingHandler.parseSubVersionValue(e, "version"));
		el.setFilename(DomHandler.getValue(e, "filename"));
		el.setFilesize(DomHandler.getValue(e, "filesize"));
		el.setFiletype(DomHandler.getValue(e, "filetype"));
		el.setDescription(DomHandler.getValue(e, "description"));
		el.setDownloads(W3CParsingHandler.parseIntValue(e, "downloads"));
		el.setSubmitDate(W3CParsingHandler.parseW3CDateValue(e, "submit_date"));
		el.setShowId(W3CParsingHandler.parseIntValue(e, "show_id"));
		el.setShowName(DomHandler.getValue(e, "show_name"));
		el.setSubmittedBy(DomHandler.getValue(e, "submitted_by"));
		
		return el;
	}


	
}
